<template>
  <view>
    <!-- 选择收货地址 -->
    <view class="addressChoose" v-if="JSON.stringify(address) === '{}'">
      <button type="warn" size="mini" class="btnChoose" @click="chooseAddress">请选择收货地址+</button>
    </view>
    <!-- 渲染收货地址 -->
    <view class="address-box" v-else @click="chooseAddress()">
      <view class="row1">
        <view class="row1-left">
          <view>收货人：{{address.userName}}</view>
        </view>
        <view class="row1-right">
          <view>电话：{{address.telNumber}}</view>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
      </view>
      <view class="row2">
        <view class="row2-left">收货地址：</view>
        <view class="row2-right">{{addressStr}}</view>
      </view>
    </view>
    <!-- 底部边框线 -->
    <image src="/static/cart_border@2x.png" class="addressBorder"></image>
  </view>
</template>

<script>
  import { mapState,mapMutations,mapGetters} from 'vuex'
  export default {
    name:"my-adress",
    data() {
      return {
        
      };
    },
    computed:{
      ...mapState('m_user',['address']),
      ...mapGetters('m_user',['addressStr']),
    },
    methods:{
      ...mapMutations('m_user',['updateAddress']),
      //选择地址按钮
      async chooseAddress(){
        const [ err, success ] = await uni.chooseAddress().catch(err => err)
        if(err === null && success.errMsg === 'chooseAddress:ok'){
          console.log('choose success')
          this.updateAddress(success)
        }
      }
    }
  }
</script>

<style lang="scss">
.addressBorder{
  display:block;
  width:100%;
  height:5px;
}
.addressChoose{
  display: flex;
  justify-content: center;
  align-items: center;
  height:90px;
}

.address-box{
  font-size:14px;
  height:90px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding:0 5px;
  .row1{
    display: flex;
    justify-content: space-between;
    .row1-right{
      display: flex;
      justify-content: space-between;
    }
  }
  
  .row2{
    margin-top:10px;
    display: flex;
    align-items: center;
    .row2.left{
      white-space: nowrap;
    }
  }
}
</style>
